package cn.bdqn.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bdqn.bean.Bill;
import cn.bdqn.bean.Provider;
import cn.bdqn.service.BillService;
import cn.bdqn.service.ProviderService;
import cn.bdqn.service.impl.BillServiceImpl;
import cn.bdqn.service.impl.ProviderServiceImpl;
import cn.bdqn.util.Constant;
import cn.bdqn.util.PageUtil;

public class BillListServlet extends HttpServlet {

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//获取供应商的信息，显示在billList.jsp页面的select中
		ProviderService proService = new ProviderServiceImpl();
		List<Provider> providerList = proService.getList();
		//把供应商list放入作用域中
		request.setAttribute("providerList", providerList);
		BillService service = new BillServiceImpl();
		PageUtil pageUtil = new PageUtil();
		Bill bill = new Bill();
		//获取查询条件信息
		String productName = request.getParameter("productName");
		String provider = request.getParameter("providerId");
		String pay = request.getParameter("isPay");
		Integer providerId = null;
		Integer isPay = null;
		if(productName!=null && productName !="" ){
			bill.setProductName(productName);
		}else{
			productName = "";//为空赋值""
		}
		if(provider!=null && provider!=""){
			providerId = Integer.parseInt(provider);
			bill.setProviderId(providerId);
		}
		if(pay!=null && pay!=""){
			isPay = Integer.parseInt(pay);
			bill.setIsPay(isPay);
		}
		//从前台获取当前页信息
		String currentPage = request.getParameter("pageIndex");
		//分页四要素
		int pageIndex = 1;
		if(currentPage != null && currentPage != ""){
			pageIndex = Integer.parseInt(currentPage);
		}
		int pageSize = Constant.PAGE_SIZE;
		int totalCount = 0;
		
		//给pageUtil赋值
		pageUtil.setPageIndex(pageIndex);
		pageUtil.setPageSize(pageSize);
		totalCount = service.getCount(bill);
		pageUtil.setTotalCount(totalCount);
		//获取总的用户信息
		//调用分页获取用户信息的方法
		List<Bill> billList = service.getList(bill, pageIndex, pageSize);
		
		//获取总页数
		int totalPages = pageUtil.getTotalPages();
		//把结果添加到作用域中
		request.setAttribute("billList", billList);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("totalPages", totalPages);
		request.setAttribute("totalCount", totalCount);
		//转发到userList.jsp
		request.getRequestDispatcher("/jsp/billList.jsp").forward(request,response);
	}

}
